/*
 * Copyright (c) 2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_IGLYPH_PROVIDER_H_INCLUDED_
#define ALKES_IGLYPH_PROVIDER_H_INCLUDED_

#include "alkes/internal.h"
#include "alkes/core/TBitArray.h"
#include "alkes/media/ImageBuffer.h"

namespace alkes {

class AL_EXPORT IGlyphProvider
    : public TRefCount<IGlyphProvider>
{
    AL_DECLARE_ROOT_CLASS(IGlyphProvider);

public:
    /*
        +------------------>
        |   (x, y)
        |   *------+
        |   |      |
        |   |      |
        |   |      |
        |   +------+
    */
    struct GlyphMetrics
    {
        uint16_t x;             // left of bounding box
        uint16_t y;             // top of bounding box
        uint16_t bbox_width;    // width of bounding box
        uint16_t bbox_height;   // height of bounding box
        uint16_t advance;
    };

    struct TextMetrics
    {
        uint16_t height;
        uint16_t max_width;
        uint16_t ascent;
        uint16_t descent;
    };

    enum Encoding
    {
        AL_ENCODING_UTF8 = 0,
        AL_ENCODING_UTF16,
        AL_ENCODING_UTF32,

#if (ALKES_API_WIN32 == 1)
        AL_ENCODING_DEFAULT = AL_ENCODING_UTF16,
#else
        AL_ENCODING_DEFAULT = AL_ENCODING_UTF32,
#endif
    };


    IGlyphProvider(Encoding encoding = AL_ENCODING_DEFAULT);
    virtual ~IGlyphProvider() = 0;

    void setEncoding(Encoding encoding);

    const TextMetrics& getTextMetrics() const;

    bool getGlyphMetrics(int32_t ch, GlyphMetrics& metrics);
    virtual bool getTextWidth(const void* text, uint16_t& width, uint16_t limit_width = 0);
    bool renderChar(int32_t ch, ImageBuffer& dst, uint16_t x, uint16_t y, GlyphMetrics& metrics);
    virtual bool renderText(const void* text, ImageBuffer& dst, uint16_t x, uint16_t y, bool clear_first = false);

protected:

    struct RenderBuffer
    {
        uint8_t* buffer;
        int32_t pitch;
        uint16_t width;
        uint16_t height;

        inline void setPixel(uint16_t x, uint16_t y, uint8_t ch)
        {
            *(buffer + (pitch * y + x)) = ch;
        }
    };

    void initTextMetrics(TextMetrics& metrics);

    virtual bool onGetGlyphMetrics(int32_t ch, GlyphMetrics& metrics) = 0;
    virtual bool onRenderChar(int32_t ch, RenderBuffer& target, GlyphMetrics& metrics) = 0;

    virtual bool lock(ImageBuffer& dst, uint16_t x, uint16_t y, RenderBuffer& target);
    virtual void unlock(ImageBuffer& dst);

private:
    friend class TRefCount<IGlyphProvider>;
    virtual void destroy() = 0;

private:
    Encoding encoding_;
    TextMetrics text_metrics_;
};

}

#endif
